package appUtils;

import android.app.Activity;
import android.app.Dialog;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.jhlibrarys.R;
import com.zhy.autolayout.AutoLinearLayout;

/**
 * Created by Joe on 2017/7/15 0015 16:10.
 */

public class CustomAlertDoubleBtnDialog {
    private final Activity mActivity;
    private commitClickListener mCommitClickListener;
    private CancelClickListener mCancelClickListener;
    private ViewHolder holder;
    private Dialog mDialog;
    private boolean inSideCanDismiss;

    public CustomAlertDoubleBtnDialog(Activity activity) {
        mActivity = activity;
        createDialog();
    }

    private void createDialog() {
        mDialog = new Dialog(mActivity, R.style.CusDialog);
        LayoutInflater inflater = LayoutInflater.from(mActivity);
        View dialogWindowView = inflater.inflate(R.layout.dialog_alert_double_btn, null);
        mDialog.setContentView(dialogWindowView);
        try {
            WindowManager.LayoutParams attributes = mDialog.getWindow().getAttributes();
            DisplayMetrics metrics = mActivity.getResources().getDisplayMetrics();
            attributes.width = (int) (metrics.widthPixels * 0.8);
            attributes.height = (int) (metrics.heightPixels * 0.8);
            mDialog.getWindow().setAttributes(attributes);
        } catch (Exception e) {
            e.printStackTrace();
            PayTokenException.getInstance().saveExceptionFile("" +
                    "CustomAlertDoubleBtnDialog getAttributes error: " + e.getMessage());
        }
        holder = new ViewHolder(dialogWindowView);
        bindClickListener();
    }

    private void bindClickListener() {
        holder.topLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (inSideCanDismiss) {
                    dismiss();
                }
            }
        });
        holder.dialogTv0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCancelClickListener != null) {
                    mCancelClickListener.onClick();
                }
                dismiss();
            }
        });
        holder.dialogTv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCommitClickListener != null) {
                    mCommitClickListener.onClick();
                }
                dismiss();
            }
        });
    }

    public void showDialog() {
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.show();
        }
    }

    public void hideLeftBtn() {
        if (holder != null) {
            holder.dialogTv0.setVisibility(View.GONE);
        }
    }

    public void hideRightBtn() {
        if (holder != null) {
            holder.dialogTv1.setVisibility(View.GONE);
        }
    }

    public void hideTitle() {
        if (holder != null) {
            holder.tvDialogTitle.setVisibility(View.GONE);
        }
    }

    public void hideBtnLayout() {
        if (holder != null) {
            holder.bottomView.setVisibility(View.GONE);
            holder.btnLayout.setVisibility(View.GONE);
        }
    }

    public void setCommitClickListener(commitClickListener listener) {
        mCommitClickListener = listener;
    }

    public void setCancelClickListener(CancelClickListener listener) {
        mCancelClickListener = listener;
    }

    public void setCanceledOnTouchInside(boolean flag) {
        inSideCanDismiss = flag;
    }

    public void setCancelAble(boolean flag) {
        mDialog.setCancelable(flag);
    }

    public void setTitle(String title) {
        if (holder != null) {
            holder.tvDialogTitle.setText(title);
        }
    }

    public void setMsg(String msg) {
        if (holder != null) {
            holder.tvDialogMsg.setText(msg);
        }
    }

    public void setLeftBtnLabel(String string) {
        if (holder != null) {
            holder.dialogTv0.setText(string);
        }
    }

    public void setRightBtnLabel(String string) {
        if (holder != null) {
            holder.dialogTv1.setText(string);
        }
    }

    public void dismiss() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    public interface commitClickListener {
        void onClick();
    }

    public interface CancelClickListener {
        void onClick();
    }

    private static class ViewHolder {
        final AutoLinearLayout topLayout;
        final TextView tvDialogTitle;
        final TextView tvDialogMsg;
        final TextView dialogTv0;
        final TextView dialogTv1;
        final AutoLinearLayout btnLayout;
        final View bottomView;

        ViewHolder(View view) {
            tvDialogTitle = (TextView) view.findViewById(R.id.tv_dialog_title);
            tvDialogMsg = (TextView) view.findViewById(R.id.tv_dialog_msg);
            dialogTv0 = (TextView) view.findViewById(R.id.dialog_tv0);
            dialogTv1 = (TextView) view.findViewById(R.id.dialog_tv1);
            btnLayout = (AutoLinearLayout) view.findViewById(R.id.dialog_btn_layout);
            topLayout = (AutoLinearLayout) view.findViewById(R.id.dialog_alert_top_layout);
            bottomView = view.findViewById(R.id.dialog_alert_view);
        }
    }
}
